from django.shortcuts import render
from web.forms.project import ProjectModelForm
from django.http import JsonResponse
from web.models import Project
from web.models import UserInfo
from web.forms.account import RegisterModelForm
from web.forms.project import ProjectModelForm2
from web.forms.project import ProjectModelForm3
from web.forms.project import ProjectModelForm4


def project_list(request):
    """ 项目列表显示 """
    if request.method == 'GET':
        form = ProjectModelForm(request)
        data = Project.objects.all()
        data1=Project.objects.filter(creator=request.user_obj)
        context = {
            'form': form,
            'data': data,
            'data1':data1
        }
        return render(request, 'project_list.html', context)
    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 添加项目
        form.instance.creator = request.user_obj
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})





def project_list2(request):
    """ 项目列表显示 """
    if request.method == 'GET':
        form = ProjectModelForm2(request)
        data = UserInfo.objects.all()
        data1=UserInfo.objects.filter(username=request.user_obj)
        context = {
            'form': form,
            'data': data,
            'data1':data1
        }
        return render(request, 'project_list2.html', context)
    form = ProjectModelForm2(request, data=request.POST)
    if form.is_valid():
        # 添加项目
        UserInfo.objects.create(
            username=request.POST.get('username'),
            password=request.POST.get('password'),
            email=request.POST.get('email'),
            mobile_phone=request.POST.get('mobile_phone')
        )
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})


def project_list3(request):
    if request.method == 'GET':
        form = ProjectModelForm3(request)
        data = Project.objects.all()
        data1 = Project.objects.filter(creator=request.user_obj)
        context = {
            'form': form,
            'data': data,
            'data1': data1
        }
        return render(request, 'project_list3.html', context)
    form = ProjectModelForm3(request, data=request.POST)
    if form.is_valid():
        # 添加项目
        Project.objects.filter(name=request.POST.get('name'),creator=request.user_obj).delete()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})

def project_list4(request):
    if request.method == 'GET':
        form = ProjectModelForm4(request)
        data = UserInfo.objects.all()
        data1 = UserInfo.objects.filter(username=request.user_obj)
        context = {
            'form': form,
            'data': data,
            'data1': data1
        }
        return render(request, 'project_list4.html', context)
    form = ProjectModelForm4(request, data=request.POST)
    if form.is_valid():
        # 添加项目
        UserInfo.objects.filter(username=request.user_obj).\
            update(
            password=request.POST.get('password'),
            email=request.POST.get('email'),
            mobile_phone=request.POST.get('mobile_phone')
                   )
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})